// pages/map/map.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    longitude: '',
    latitude: '',
    MapContext: {},
    markers: [],
    allMarkers: [],
    is_move: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.data.MapContext = wx.createMapContext('myMap')
    var that = this;
    //获取当前地理位置
    wx.getSetting({
      success(res) {
        if (!res.authSetting['scope.userLocation']) {
          wx.authorize({
            scope: 'scope.userLocation',
            success () {
              // 用户已经同意小程序使用录音功能，后续调用 wx.startRecord 接口不会弹窗询问
              that.getNewLocation();
            }
          })
        }else{
          that.getNewLocation();
        }
      }
    })
  },

  //获取中心位置坐标
  getNewLocation: function(){
    var that = this;
    wx.getLocation({
      type: 'gcj02',
      success: function(res){
        //console.log(res)
        that.data.longitude = res.longitude;
        that.data.latitude = res.latitude;
        that.setData({
          longitude: res.longitude,
          latitude: res.latitude
        });
        that.getDataInfoList(res.latitude, res.longitude);
      }
    })
  },

  //返回当前位置
  moveToLocation: function(){
    this.data.MapContext.moveToLocation()
    this.setData({is_move: false});
    this.getDataInfoList(this.data.latitude, this.data.longitude);
  },

  //获取当前中心坐标
  getBoundary: function(){
    var that = this;
    this.data.MapContext.getCenterLocation({
      success: function(res){
        //console.log(res)
        that.getDataInfoList(res.latitude, res.longitude);
      }
    })
  },

  //监控地图移动
  regionchange(e) {
    console.log(e)
    var that = this
    if(e.type == 'end' && e.causedBy == 'drag'){  //e.causedBy只有第一次加载时值才会为scale
      that.getBoundary()
      that.setData({is_move: true});
    }
    this.setData({nowData: false});
  },

  //获取1000米内信息
  getDataInfoList: function(latitude, longitude){
    var that = this;
    //var location = 'rectangle('+res.southwest.latitude+','+res.southwest.longitude+','+res.northeast.latitude+','+res.northeast.longitude+')';
    var location = 'nearby('+latitude+','+longitude+',1000)';
    var url = app.globalData.mapUrl + 'ws/place/v1/search?keyword=' + encodeURI('宠物医院') + '&key=' + app.globalData.mapKey + '&boundary=' + location;
    
    //var url = 'https://apis.map.qq.com/ws/place/v1/search?keyword=%E5%AE%A0%E7%89%A9%E5%8C%BB%E9%99%A2&key=TODBZ-I6BWD-P6B4Y-H7IJT-DTBTK-GZFGW&boundary='+location;
    wx.request({
      url: url,
      data: {page_size: 20},
      success: function(rest){
        console.log(rest)
        that.data.markers = rest.data.data;
        var markers = [];
        for (var value of rest.data.data) {
          if(typeof value.tel == "undefined" || value.tel == null || value.tel == " "){
            var content = value.title;
          }else{
            var content = value.title+'('+value.tel+')';
          }
          
          var temp = {
            id: value.id,
            latitude: value.location.lat,
            longitude: value.location.lng,
            iconPath: 'http://php.bkybk.com/static/images/wx/adress.png',
            width: '30rpx',
            height: '40rpx',
            callout: {
              content: content,
              borderRadius: 3,
              padding: '10rpx',
              display: 'BYCLICK',
              textAlign: 'center',
              borderColor: '#E9E9E9',
              anchorY: '-10',
              borderWidth: '1'
            }
          };
          markers.push(temp); 
        }
        //console.log(that.data.markers)
        that.data.allMarkers = rest.data.data;
        that.setData({markers: markers});
      }
    })
  },

  markertap: function(e){
    var id = e.markerId;
    //遍历所有存在坐标点，获取当前坐标点信息
    for (var value of this.data.allMarkers) {
      if(id == value.id){
        console.log(value)
        this.setData({nowData: value});
      }
    }
  },

  /**
   * 到这去
   */
  goToDestination: function(event){
    console.log(event);
    wx.openLocation({
      latitude: event.currentTarget.dataset.markvalue.location.lat,
      longitude: event.currentTarget.dataset.markvalue.location.lng,
      scale: 18,
      name: event.currentTarget.dataset.markvalue.title,
      address: event.currentTarget.dataset.markvalue.address,
      success: function(data){
        console.log(data)
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})